动态规划:凑硬币(中级:动态规划思想体会)

解题心得:
1、对于动态规划,并不是简单的套公式,自己的思想是第一位。首先应该自己去想解决问题的方法,用动态规划去理解题,抓住真正的转移点,扩大点,公式会很自然的出来。转移状态的方程式很多变,并不是固定不动的。
2、此题在动态转移的时候使用的是二维数组,所以方程式是多变的,思想也在变动。
3、在结果固定不变的题可以先打表,得出所有的答案,在多次询问的时候直接从表中的到结果,这样可以节省时间。
4、在发现自己的思路不对时,不要用公式去强行套,改变思维。

题目:
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.
For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, two 5-cent coins and one 1-cent coin, one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.
Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 7489 cents. Input The input file contains any number of lines, each one consisting of a number for the amount of money in cents. Output For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.
Sample Input 11 26
Sample Output 4 13

#include<stdio.h>

int table[7500][5],money,va[5]={1,5,10,25,50};
void money_table()
{
        //也可以是面额转移,钱不变,所以是dp灵活的是思想,不是公式。
        for(int j=1;j<7500;j++)//钱逐步扩大,状态转移。
            for(int i=1;i<5;i++)//分别使用5、10、25、50的钱去凑(1元在主函数里已经使用)。
                for(int k=0;k*va[i]<=j;k++)//注意不能缺少等号。
                    table[j][i] += table[j-k*va[i]][i-1];//1元的加上用5元的加上10元的慢慢的加,最后得到最终的答案。
}
int main()
{
        for(int i=0;i<7500;i++)
            table[i][0] = 1;//当只使用一元的时候,无论多少money都只有一种方法。
    money_table();
    while(scanf("%d",&money)!=EOF)
    {
        printf("%d\n",table[money][4]);//table[money][4]最后得到的是加到了第50元上。
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值